/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package testxml;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;

/**
 *
 * @author marcinse
 */
public class XMLhandler
{
    private InputStream in;
    private XMLStreamReader streamReader;
    private static String ITEM = "item";
    private static String NAME = "name";
    private static String DURATION = "Duration";
    private static String ITEMKNOWLEDGE = "ItemKnowledge";
    
    XMLhandler()
    {
        XMLInputFactory factory = XMLInputFactory.newInstance();
        try
        {
            in = new FileInputStream("Items.xml");
            streamReader = factory.createXMLStreamReader(in);
        }
        catch (XMLStreamException | FileNotFoundException ex)
        {
            Logger.getLogger(XMLhandler.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    ArrayList loadItems()
    {
        int i = 0;
        Item temp = null;
        ArrayList ItemList = new ArrayList();
        try
        {
            while(streamReader.hasNext())
            {
                int event = streamReader.next();
                
                if(streamReader.getEventType() == XMLStreamReader.START_ELEMENT)
                {
                    if(streamReader.getLocalName().equals(ITEM))
                    {
                        ItemList.add(new Item());
                        temp = (Item) ItemList.get(i);
                        i++;
                    }
                    if(streamReader.getLocalName().equals(NAME))
                    {
                       // System.out.println(streamReader.getElementText());
                        temp.setName(streamReader.getElementText());
                        
                    }
                    if(streamReader.getLocalName().equals(DURATION))
                    {
                     //   System.out.println(streamReader.getElementText());
                        temp.setDuration(Short.valueOf(streamReader.getElementText()));
                    }
                    if(streamReader.getLocalName().equals(ITEMKNOWLEDGE))
                    {
                        temp.setItemKnowledge(Float.valueOf(streamReader.getElementText()));
                    }
                    
                    
                    
                    
                    
              //      System.out.println(streamReader.getLocalName());
             //       int i = streamReader.getEventType();
            //        System.out.println(i);
             //       System.out.println(streamReader.getAttributeLocalName(i-1));
             //       System.out.println();
                }
               
            }
        }
        catch (XMLStreamException ex)
        {
            Logger.getLogger(XMLhandler.class.getName()).log(Level.SEVERE, null, ex);
        }
        return ItemList;
    }  
}